<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind" label-width="100px">
      <uni-forms-item name="group_id" label="所属箱组" required>
        <uni-data-select
            collection="mydb-locker-group-list"
            field="_id as value, name as text"
            v-model="formData.group_id"
            :clear="false"
          />
      </uni-forms-item>
      <uni-forms-item name="cell_code" label="格子编号" required>
        <uni-easyinput v-model="formData.cell_code"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="size_type" label="尺寸类型" required>
        <uni-data-checkbox v-model="formData.size_type" :localdata="formOptions.size_type_localdata"></uni-data-checkbox>
      </uni-forms-item>

      <uni-forms-item name="qr_code" label="开箱二维码">
        <uni-easyinput v-model="formData.qr_code"></uni-easyinput>
      </uni-forms-item>
	  <uni-forms-item name="last_used" label="最后使用时间">
	    <uni-datetime-picker return-type="timestamp" v-model="formData.last_used"></uni-datetime-picker>
	  </uni-forms-item>
	  <uni-forms-item name="remark" label="其他备注">
	    <uni-easyinput v-model="formData.remark"></uni-easyinput>
	  </uni-forms-item>
	  <uni-forms-item name="status" label="状态" required>
	    <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
	  </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/mydb-locker-cell-list.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mydb-locker-cell-list';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "group_id": "",
        "cell_code": "",
        "size_type": null,
        "status": 1,
        "last_used": null,
        "qr_code": "",
        "create_time": null,
        "update_time": null,
		"remark":""
      }
      return {
        formData,
        formOptions: {
          "size_type_localdata": [
            {
              "value": 0,
              "text": '小'
            },
            {
              "value": 1,
              "text": '中'
            },
            {
              "value": 2,
              "text": '大'
            }
          ],
          "status_localdata": [
            {
              "value": 0,
              "text": '禁用'
            },
            {
              "value": 1,
              "text": '启用'
            },
            {
              "value": 2,
              "text": '已占用'
            },
            {
              "value": 3,
              "text": '已停用'
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("group_id,cell_code,size_type,status,last_used,qr_code,create_time,update_time").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
